package io.kiki.sba.registry.server.clientInterface.filter.blacklist;

import io.kiki.sba.registry.server.clientInterface.filter.IPMatchStrategy;
import io.kiki.sba.registry.server.clientInterface.providedata.FetchBlackListService;
import org.apache.commons.lang.StringUtils;

import javax.annotation.Resource;
import java.util.List;
import java.util.Set;
import java.util.function.Supplier;


public class DefaultIPMatchStrategy implements IPMatchStrategy<String> {

    @Resource
    private FetchBlackListService fetchBlackListService;

    @Override
    public boolean match(String IP, Supplier<String> getOperatorType) {
        return match(getOperatorType.get(), IP);
    }

    private boolean match(String type, String matchPattern) {

        List<BlacklistConfig> blacklistConfigList = fetchBlackListService.getBlacklistConfigList();
        for (BlacklistConfig blacklistConfig : blacklistConfigList) {

            // 如黑名单类型不匹配则跳过
            if (!StringUtils.equals(type, blacklistConfig.getType())) {
                continue;
            }

            List<MatchType> matchTypeList = blacklistConfig.getMatchTypes();

            // 匹配规则为空跳过
            if (null == matchTypeList || matchTypeList.size() == 0) {
                continue;
            }

            for (MatchType matchType : matchTypeList) {
                if (null == matchType) {
                    continue;
                }

                if (BlacklistConstants.IP_FULL.equals(matchType.getType())) {
                    // IP 全匹配时判断当前发布者IP是否在IP列表中，如命中则拒绝发布
                    @SuppressWarnings("unchecked") Set<String> patterns = matchType.getPatternSet();

                    if (null == patterns || patterns.size() == 0) {
                        continue;
                    }

                    if (patterns.contains(matchPattern)) {
                        return true;
                    }
                }
            }
        }
        return false;
    }


}
